package cease.butter;

import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSON;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import cease.urs.LoginException;
import cease.urs.Loginer;
import cease.urs.Session;
import cease.vo.Butter;

/**
 * @author dhf
 */
public class ButterExecutor extends BaseExecutor {
    private static ButterExecutor executor = new ButterExecutor();

    public static ButterExecutor getInstance() {
        return executor;
    }

    public Butter post(Session session, String text, String inReplyToStatusId,
            String source) throws ApiException {
        if (StringUtils.isBlank(text)) {
            throw new IllegalArgumentException("[text] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("status", text));
        if (null != inReplyToStatusId) {
            pairs.add(new BasicNameValuePair("inReplyToButterID",
                    inReplyToStatusId));
            pairs.add(new BasicNameValuePair("butterId", inReplyToStatusId));
        }
        if (null != source) {
            pairs.add(new BasicNameValuePair("source", source));
        }
        JSON json = post(Constants.URL_BUTTER_POST, pairs, null, session);
        return toButter(json);
    }

    public Butter retweet(Session session, String butterId) throws ApiException {
        if (StringUtils.isBlank(butterId)) {
            throw new IllegalArgumentException("[butterId] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("id", butterId));
        JSON json = post(Constants.URL_BUTTER_RETWEET, pairs, null, session);
        return toButter(json);
    }

    public Butter delete(Session session, String butterId) throws ApiException {
        if (StringUtils.isBlank(butterId)) {
            throw new IllegalArgumentException("[butterId] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("id", butterId));
        JSON json = post(Constants.URL_BUTTER_DELETE, pairs, null, session);
        return toButter(json);
    }

    public Butter show(Session session, String butterId) throws ApiException {
        if (StringUtils.isBlank(butterId)) {
            throw new IllegalArgumentException("[butterId] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("id", butterId));
        JSON json = get(Constants.URL_BUTTER_SHOW, pairs, null, session);
        return toButter(json);
    }

    public Butter mute(Session session, String butterId) throws ApiException {
        if (StringUtils.isBlank(butterId)) {
            throw new IllegalArgumentException("[butterId] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("butterID", butterId));
        JSON json = post(Constants.URL_BUTTER_MUTE, pairs, null, session);
        return toButter(json);
    }

    public Butter unmute(Session session, String butterId) throws ApiException {
        if (StringUtils.isBlank(butterId)) {
            throw new IllegalArgumentException("[butterId] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("butterID", butterId));
        JSON json = post(Constants.URL_BUTTER_UNMUTE, pairs, null, session);
        return toButter(json);
    }

    public static void main(String[] args) throws LoginException, ApiException {
        Session session = Loginer.login("dhf@126.com", "wrongpassword");
        // 发微博
        System.out.println(executor.post(session, "hello world!", null, null));
        // 转发
        System.out.println(executor.retweet(session, "9150817849589595007"));
        // 删除微博/取消转发
        System.out.println(executor.delete(session, "9150817849589595007"));
    }
}
